﻿using ETA.DomainEnum;
using ETA.Entity;
using ETA.IService;
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.ClientService
{
    public class RegistrationMainFormBll
    {
        private static IRegistrationMainFormService _IRegistrationMainFormService { set; get; }
        private static IFlowStepService _IFlowStepService { set; get; }
        private static IFlowLogService _IFlowLogService { set; get; }

        static RegistrationMainFormBll()
        {
            IKernel kernel = KernelHelper.GetKernel();
            _IRegistrationMainFormService = kernel.Get<IRegistrationMainFormService>();
            _IFlowStepService = kernel.Get<IFlowStepService>();
            _IFlowLogService = kernel.Get<IFlowLogService>();
        }

        /// <summary>
        /// 提交流程
        /// </summary>
        /// <param name="flowIndex">步骤id</param>
        /// <returns></returns>
        public static Response SubmitFlowLog(string userId, int flowIndex, FlowLogStateEnum flowLogStateEnum)
        {
            var res = new Response();
            var flowStep = new FlowStep();
            var flowStepResponse = _IFlowStepService.QueryFlowStepList(new QueryFlowStepRequest
            {
                FlowStep = new FlowStep
                {
                    FlowIndex = flowIndex
                }
            });
            if (flowStepResponse.Result.IsSuccess)
            {
                flowStep = flowStepResponse.FlowSteps.FirstOrDefault();
            }

            var user = UserBll.GetCompltetUserInfo(userId);
            var mainForm = user.RegistrationMainForms.FirstOrDefault();
            var count = 0;
            var flowLogQueryRes = _IFlowLogService.QueryFlowLog(new QueryFlowLogRequest
            {
                FlowLog = new FlowLog
                {
                    RegistrationMainFormId = mainForm.RegistrationMainFormId,
                    FlowStepId = flowStep.FlowStepId
                }
            }, out count);
            res = IsEnableSummit(userId, flowIndex);
            if (!res.Result.IsSuccess)
            {
                //无法提交
                return res;
            }

            var flowLogId = string.Empty;
            if (flowLogQueryRes.Result.IsSuccess && count > 0)
            {
                //已存在流程记录则更新 modifyDate
                var log = flowLogQueryRes.OrgFlowLogs.First();
                log.ModifyDate = DateTime.Now;
                log.SourceEnum = SourceEnum.网站;
                log.HandlerStateEnum = HandlerStateEnum.未处理;
                log.FlowLogStateEnum = flowLogStateEnum;
                res = _IFlowLogService.ModifyFlowLog(new ModifyFlowLogRequest
                {
                    FlowLog = log
                });
                flowLogId = log.FlowLogId;
            }
            else
            {
                //新增
                var log = new FlowLog();
                log.FlowLogId = Guid.NewGuid() + "";
                log.RegistrationMainForm = user.RegistrationMainForms.FirstOrDefault();
                log.FlowStepInfo = flowStep;
                log.SourceEnum = DomainEnum.SourceEnum.网站;
                log.FlowLogStateEnum = flowLogStateEnum;
                log.HandlerStateEnum = HandlerStateEnum.未处理;
                var createLogReq = new CreateFlowLogRequest
                {
                    FlowLog = log
                };
                res = _IFlowLogService.CreateFlowLog(createLogReq);
                flowLogId = log.FlowLogId;
            }

            if (res.Result.IsSuccess)
            {
                res.Result.IsSuccess = _IRegistrationMainFormService.UpdateCurrentStep(userId, flowStep.FlowStepId);
                if (!res.Result.IsSuccess)
                {
                    res.Result.ErrMessage = "更新当前步骤失败";
                }
                else
                {
                    res.Result.Message = flowLogId;
                }
            }
            return res;
        }

        /// <summary>
        /// 检查是否可以提交
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="flowIndex"></param>
        /// <returns></returns>
        public static Response IsEnableSummit(string userId, int flowIndex)
        {
            List<FlowStep> flowStatus = null;
            var response = _IRegistrationMainFormService.QueryRegistrationMainFormList(new QueryRegistrationMainFormRequest
            {
                UserId = userId
            });
            flowStatus = response.RegistrationMainForms.First().FlowStepStatus.ToList();
            var res = new Response();
            var flowStepInfo = flowStatus.Find(l => l.FlowIndex == flowIndex);
            if (flowStepInfo.StepStatusEnum == StepStatusEnum.已完成 || flowStepInfo.StepStatusEnum == StepStatusEnum.不可开启)
            {
                res.Result = new Dtos.ResultDto
                {
                    IsSuccess = false,
                    ErrMessage = "该流程已经完成，或者不可开启,无法提交"
                };
            }
            else
            {
                res.Result.IsSuccess = true;
            }
            return res;
        }
    }
}
